<style lang="less" scoped>
.notice {
  .ivu-tabs-bar {
    margin: 0;
  }
  .smallForm {
    display: inline-block;
  }
}
.width90 {
  width: 95%;
}
.redStar {
  color: red;
  font-size: 22px;
}
</style>


<template>
    <div class="notice">
        <Card :bordered="false">
            <p slot="title" class="white">通告管理</p>
            <div>
                <Form :model="searchForm" class="smallForm">
                    <Input v-model="seacrh" style="width:200px;" placeholder="请输入通告标题"></Input>
                </Form>
                <Button type="info" style="margin-left:20px" @click="getNotice('1')">搜索</Button>
            </div>
        </Card>
        <Table :data="noticeData" @on-selection-change="showSelect" :columns="noticeColumns2" stripe></Table>
        <Page
            class="pull-right"
            :total="Page.total"
            :current="Page.current"
            :page-size="Page.pageSize"
            size="small"
            show-total
            @on-change="changePage"
        ></Page>
        <Modal title="通告" width="600" v-model="noticeModal2">
            <Form :label-width="60">
                <Form-item label="标题" prop="title">
                    <Input v-model="title" :maxlength="35" class="width90"></Input><span class="redStar">*</span>
                </Form-item>
                <Form-item label="正文" prop="text">
                    <Input v-model="text" type="textarea" class="width90"></Input><span class="redStar">*</span>
                </Form-item>
                <Form-item label="接收部门" prop="dep_name" class="width90">
                    <Cascader :data="areaArr" placeholder="请选择部门" @on-change="showAddress" v-model="dep_name"></Cascader>
                </Form-item>
                <!-- <Table :data="noticeDaoru" v-if="noticeDaoru.length > 0" :columns="noticeDaoruColumns2" stripe></Table>   -->
            </Form>
        </Modal>
    </div>
</template>

<script>
import { ajaxPost } from 'global/axios'
import storage from 'good-storage'
import {domain, domain2} from 'global/setting'
export default {
  data () {
    return {
      urlNoticeList: '/sys/notice/list',
      urlNoticeNew: '/sys/notice/new',
      urlNoticeDel: '/sys/notice/del',
      urlAddressList: '/sys/department/getTree',
      urlRead: '/sys/notice/read',
      urlNoticeEdit: '/sys/notice/new',
      searchForm: {},
      seacrh: '',
      noticeColumns2: [
        {
          title: '标题',
          key: 'title'
        },
        {
          title: '发送人',
          key: 'realname'
        },
        {
          title: '接受部门',
          key: 'dep_name'
        },
        {
          title: '发送日期',
          key: 'send_date'
        },
        {
          title: '正文',
          key: 'text'
        },
        {
          title: '操作',
          render: (h, param) => {
            let read = false
            for (let i = 0; i < param.row.receive_users.length; i++) {
              if (
                param.row.receive_users[i].username == storage.session.get('username')
              ) {
                if (param.row.receive_users[i].is_receive == '0') {
                  read = false
                } else {
                  read = true
                }
                break
              } else {
                read = true
              }
            }
            if (!read) {
              return h('div', [
                h(
                  'Button',
                  {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        this.edit2(param.row)
                      }
                    }
                  },
                  '查看详细信息'
                ),
                h(
                  'Button',
                  {
                    props: {
                      type: 'info',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        let params = {
                          data: {
                            _id: param.row._id
                          },
                          url: this.urlRead,
                          token: storage.session.get('token'),
                          success: res => {
                            if (res.data.code == '0000') {
                              this.$Message.success({
                                content: '确认阅读'
                              })
                              this.getNotice('1')
                            } else {
                              this.$Message.warning({
                                content: res.data.message
                              })
                            }
                          }
                        }
                        ajaxPost(params)
                      }
                    }
                  },
                  '确认已读'
                )
              ])
            } else {
              return h('div', [
                h(
                  'Button',
                  {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        this.edit2(param.row)
                      }
                    }
                  },
                  '查看详细信息'
                )
              ])
            }
          }
        }
      ],
      Page: {
        current: 1,
        pageSize: 13,
        total: 0
      },
      noticeModal: false,
      noticeModal2: false,
      daoruUrl: '',
      noticeDaoru: [],

      actionPage: '1',
      totalSize: '',
      title: '',
      text: '',
      btnSave: true,
      areaArr: [],
      addressData: [],
      dep_name: [],
      modal2: false,
      selectData: [],
      old_id: '',
      noticeData: []
    }
  },
  methods: {
    download (row) {
      window.open(domain2 + row.path)
    },
    changePage (page) {
      this.actionPage = page
      this.getNotice(page)
    },
    getNotice (page = 1) {
      let params = {
        data: {
          searchValue: {
            dep_name: storage.session.get('dep_name')
          },
          order: { send_date: -1 },
          page: page,
          pageSize: this.Page.pageSize
        },
        url: this.urlNoticeList,
        token: storage.session.get('token'),
        success: res => {
          this.noticeData = res.data.result.result
          this.Page.total = res.data.result.totalSize
          this.totalSize = res.data.result.totalSize
        }
      }
      if (this.seacrh !== '') {
        params.data.searchValue.title = this.seacrh + '|#|like'
      }
      ajaxPost(params)
    },
    edit2 (row) {
      this.noticeModal2 = true
      this.title = row.title
      this.text = row.text
      this.dep_name = row.dep_array
    },
    getAddress () {
      var that = this
      var params = {}
      params.data = {}
      params.token = storage.session.get('token')
      params.url = this.urlAddressList
      params.success = function (res) {
        that.areaArr = res.data.result
      }
      ajaxPost(params)
    },
    showAddress (data) {
      this.addressData = data
    },
    showSelect (data) {
      this.selectData = data // 把勾选的数组存起来
    }
  },
  mounted () {
    this.daoruUrl = domain + '/filemanage/upload?dir=notice&token=' + storage.session.get('token')
    this.getAddress()
  },
  created () {
    this.getNotice()
  }
}
</script>
